import type { Menu } from "@/typings";

const isArray = Array.isArray;
const keys = Object.keys;
const typeMap = new Map<string, string>([
  ["string", "string"],
  ["number", "number"],
  ["boolean", "boolena"],
  ["bigint", "bigint"],
  ["symbol", "symbol"]
]);

/**
 * @description 获取浏览器默认语言
 * @returns {String}
 */
export function getBrowserLanguage(): string {
  const browserLang = window.navigator.language;
  let defaultBrowserLang = "";
  if (["cn", "zh", "zh-cn"].includes(browserLang.toLowerCase())) {
    defaultBrowserLang = "zh";
  } else {
    defaultBrowserLang = "en";
  }
  return defaultBrowserLang;
}

/**
 * @description 使用递归找出所有面包屑存储到 pinia/vuex 中
 * @param {Menu[]} menuList 菜单列表
 * @param {Array} parent 父级菜单
 * @param {Object} result 处理后的结果
 * @returns {Object}
 */
export const getAllBreadcrumbList = (menuList: Menu[], parent = [], result: { [key: string]: any } = {}) => {
  for (const item of menuList) {
    result[item.path] = [...parent, item];
    if (item.children) getAllBreadcrumbList(item.children, result[item.path], result);
  }
  return result;
};

/**
 * @description 使用递归扁平化菜单，方便加动态路由
 * @param {Array} menuList 菜单列表添
 * @returns {Array}
 */
export function getFlatMenuList(menuList: Menu[]): Menu[] {
  const newMenuList: Menu[] = JSON.parse(JSON.stringify(menuList));
  return newMenuList.flatMap((item) => [item, ...(item.children ? getFlatMenuList(item.children) : [])]);
}

/**
 * @description 防抖
 * @param func 回调
 * @param delay 时间
 * @returns {T}
 */
export function debounce<T extends (...args: any[]) => any>(func: T, delay: number = 0, context?: any): T {
  let timeoutId: number | undefined;
  const debouncedFunc = function (...args: any[]) {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() => {
      func.apply(context, args);
    }, delay);
  };
  return debouncedFunc as T;
}

/**
 * @description 获取当前时间对应的提示语
 * @returns {String}
 */
export function getTimeState() {
  const timeNow = new Date();
  const hours = timeNow.getHours();
  if (hours >= 6 && hours <= 10) return `早上好呀！ ⛅`;
  if (hours >= 10 && hours <= 14) return `中午好呀！ 🌞`;
  if (hours >= 14 && hours <= 18) return `下午好呀！ 🌞`;
  if (hours >= 18 && hours <= 24) return `晚上好呀！ 🌛`;
  if (hours >= 0 && hours <= 6) return `凌晨好呀！ 🌛`;
}

/**
 * @description 深克隆
 * @param obj 克隆对象
 * @returns 返回新对象
 */
export function deepClone<T>(obj: T): T {
  if (obj === null || typeof obj !== "object") {
    return obj;
  }
  if (obj instanceof Date) {
    return new Date(obj.getTime()) as any;
  }
  if (obj instanceof Array) {
    const newArr = [];
    for (let i = 0; i < obj.length; i++) {
      newArr[i] = deepClone(obj[i]);
    }
    return newArr as any;
  }
  if (obj instanceof Object) {
    const newObj: any = {};
    for (const key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        newObj[key] = deepClone(obj[key]);
      }
    }
    return newObj as any;
  }
  return obj;
}

/**
 * @description 获取随机数
 * @param {number} start 开始位置
 * @param {number} end 结束位置
 * @param {number} decimal 小数位数 默认0
 * @returns {number}
 */
export const random = (start: number, end: number, decimal: number = 0): number => {
  if (start >= end) return end;
  const range = end - start;
  const rendomDecimal = Math.random() * range;
  const result = Number(rendomDecimal.toFixed(decimal));
  return Math.min(start, end) + result;
};
